#include "ch02.h"
#include "y_or_n_ques.c"

int main(int argc, char *argv[])
{
    char buf[132];
    FILE *fd;
    fpos_t pos;
    if(!y_or_n_ques("Should we use append mode?"))
    {
        if((fd = fopen("test_file", "w+")) == NULL)
        {
            err_exit("fopen w+ failed");
        }
    }
    else
    {
        if((fd = fopen("test_file", "a+")) == NULL)
        {
            err_exit("fopen a+ failed");
        }
    }
    // 写入一行数据
    fputs("0123456789ABCDEFGHIJ", fd);
    // 查看当前文件尾位置
    fseek(fd, 0, SEEK_END);
    // 将文件的当前位置存入pos
    fgetpos(fd, &pos);
    printf("current file position is %ld\n", pos);
    
    fseek(fd, 30, SEEK_END);
    fgetpos(fd, &pos);
    printf("Now we call fseek(fd, 30, SEEK_END);"
    "current file position is %ld\n", pos);
    // 写入数据
    fputs("abcdefg", fd);
    printf("Now we write 7 bytes\"%s\"\n", "abcdefg");
    fgetpos(fd, &pos);
    printf("current file position is %ld\n", pos);
    fclose(fd);
    return 0;
}